var materialGuid = GetUrlParam("materialGuid");	//	材料id
var mName = GetUrlParam("mName");					//	名称
var mLabel = GetUrlParam("mLabel");				//	类型
var mParameter = GetUrlParam("mParameter");		//	类型
var mBigtype = GetUrlParam("mBigtype");			//	大类
var count = null;
$(function () {
	$("#mName").val(mName);	//	设定值
	$("#mLabel").val(mLabel);

    $("#jqGrid").jqGrid({
        url: baseURL + 'bss/dmaterialmodelitem/list',
        datatype: "json",
        colModel: [			
			{ label: 'guid', name: 'guid', index: 'guid', width: 50, key: true },
			{ label: '模块-guid', name: 'modelGuid', index: 'model_guid', width: 80 }, 			
			{ label: '材料-guid', name: 'materialGuid', index: 'material_guid', width: 80 }, 			
			{ label: '数量', name: 'count', index: 'count', width: 80 }, 			
			{ label: '', name: 'datastate', index: 'datastate', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

//	返回添加的材料数据
var callBackdata = function () {
	count = $("#count").val();
	var data = {
		"mParameter":mParameter ,
		"materialGuid":materialGuid	,
		"mName":mName	,
		"mLabel":mLabel	,
		"count":count	,
		"mBigtype":mBigtype
	};
	return data;
}



var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		dMaterialModelItem: {},
        materialGuid:materialGuid
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.dMaterialModelItem = {};

		},
		update: function (event) {
			var guid = getSelectedRow();
			if(guid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(guid)
		},
		saveOrUpdate: function (event) {
			var url = vm.dMaterialModelItem.guid == null ? "bss/dmaterialmodelitem/save" : "bss/dmaterialmodelitem/update";
            vm.dMaterialModelItem.materialGuid = materialGuid;
            vm.dMaterialModelItem.datastate = "新增";
			$.ajax({
				type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.dMaterialModelItem),
			    success: function(r){
			    	if(r.code === 0){
			    		layer.msg("操作成功");

						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var guids = getSelectedRows();
			if(guids == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "bss/dmaterialmodelitem/delete",
                    contentType: "application/json",
				    data: JSON.stringify(guids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(guid){
			$.get(baseURL + "bss/dmaterialmodelitem/info/"+guid, function(r){
                vm.dMaterialModelItem = r.dMaterialModelItem;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
		}
	}
});